//package com.addplus.townmall.server.security.config.shiro;
//
//
//
//import com.addplus.townmall.server.api.mapper.authority.MenuElementMapper;
//import com.addplus.townmall.server.api.mapper.town.PartnerMapper;
//import com.addplus.townmall.server.api.model.town.Partner;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.util.ByteSource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//
//import java.util.Arrays;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * 类名: PartnerLoginShiroRealm
// *
// * @author dengrenning
// * @version V1.0
// * @date 2017-10-17 17:00:15
// * @description 类描述: 登录基本鉴权器
// */
//public class PartnerLoginShiroRealm extends AuthorizingRealm {
//
//    @Autowired
//    private PartnerMapper partnerMapper;
//
//    @Autowired
//    private MenuElementMapper menuElementMapper;
//
//    @Value("${spring.redis.expire}")
//    private int expire;
//
//    // 授权
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        Partner partner = (Partner) SecurityUtils.getSubject().getPrincipal();
//        // 查询用户角色，拥有什么权限，放到SimpleAuthorizationInfo里面quide 、
//        // 权限信息对象info,用来存放查出的用户的所有的角色（role）及权限（permission）
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        if (partner != null) {
//            if (StringUtils.isNotBlank(partner.getRoles())) {
//                List<String> elementList = null;
//                String[] roles = partner.getRoles().split(",");
//                info.addRoles(Arrays.asList(roles));
//                partner = partnerMapper.selectById(partner.getId());
//                if (partner != null) {
//                    List<String> list = Arrays.asList(partner.getRoles().split(","));
//                    if (!list.isEmpty()) {
//                        List<String> menuElements = menuElementMapper.getUserFunctionList(list, LoginType.USER.getType());
//                        if (menuElements != null && !menuElements.isEmpty()) {
//                            info.addStringPermissions(elementList);
//                        }
//                    }
//                }
//            }
//        }
//        return info;
//    }
//
//    // 认证
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
//            throws AuthenticationException {
//        // 获取用户的输入的账号.
//        String account = (String) authenticationToken.getPrincipal();
//        QueryWrapper<Partner> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account", account);
//        queryWrapper.eq("is_delete", 0);
//        Partner shopManager = partnerMapper.selectOne(queryWrapper);
//        if (shopManager == null) {
//            throw new UnknownAccountException();
//        }
//        if (1 == shopManager.getStatus()) {
//            // 帐号锁定
//            throw new LockedAccountException();
//        }
//        Session session = SecurityUtils.getSubject().getSession();
//        session.setTimeout(expire * 1000L);
//        session.setAttribute("id", shopManager.getId());
//        session.setAttribute("loginType", LoginType.USER.getType());
//        String rolesString = shopManager.getRoles();
//        if (StringUtils.isNotBlank(rolesString)) {
//            List<String> roles = Arrays.asList(rolesString.split(","));
//            if (!roles.isEmpty()) {
//                List<Integer> list = roles.stream().filter(o -> StringUtils.isNotBlank(o)).map(o -> Integer.parseInt(o)).collect(Collectors.toList());
//                session.setAttribute("roles", list);
//            }
//        }
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(shopManager, shopManager.getPassword(), ByteSource.Util.bytes(shopManager.getPasswordSalt()), getName());
//        return authenticationInfo;
//    }
//}